﻿using LaunchAllaround.DesignPattern.MVC;
using LaunchAllaround.Pad;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchAllaround.Playlist
{
    /// <summary>
    /// Logique d'interaction pour PlaylistMenu.xaml
    /// </summary>
    public partial class PlaylistMenu : UserControl, View<PlaylistModel>, Controller<PlaylistModel>
    {

        private Model<PlaylistModel> model;

        public PlaylistMenu()
        {
            this.model = new PlaylistModel(this);
            InitializeComponent();
        }

        public PlaylistMenu(PlaylistModel model)
        {
            this.model = model;
            InitializeComponent();
        }

        public void Refresh(RefreshEvent<PlaylistModel> ev)
        {
            PadMenu pm = (PadMenu)((PlaylistModel)this.GetModel()).Get(0).GetViews().Get(1);
            this.padMenu.Children.Clear();
            this.padMenu.Children.Add(pm);
        }

        public Model<PlaylistModel> GetModel()
        {
            return this.model;
        }

        public void SetModel(Model<PlaylistModel> m)
        {
            this.model = m;
        }

        private void BeginHighlight(object sender, MouseEventArgs e)
        {
            ((Image)sender).Opacity = 1;
        }

        private void StopHighlight(object sender, MouseEventArgs e)
        {
            ((Image)sender).Opacity = 0.5;
        }

        private void AddPadEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PlaylistModel)this.GetModel()).Count < PlaylistModel.MaxPadCount)
            {
                ((PlaylistModel)this.GetModel()).Add(new PadModel());
            }
            else
            {
                Footer.myself.PrintError("You can't add a Pad, the maximal number has been reached");
            }

        }

        private void RemovePadEvent(object sender, MouseButtonEventArgs e)
        {
            if (((PlaylistModel)this.GetModel()).Count > 1)
            {
                ((PlaylistModel)this.GetModel()).RemoveAt(0);
            }
            else
            {
                Footer.myself.PrintError("You can't remove this Pad, the minimum number has been reached");
            }
        }

        private void SaveEvent(object sender, MouseButtonEventArgs e)
        {   
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "laaPlaylist";
            dlg.Filter = "Launch All Around Playlist (.laa)|*.laa";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                //sauvegarde à implémenter
                string filename = dlg.FileName;
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, this.model);
                stream.Close();
            }
        }

        private void LoadEvent(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "laaPlaylist";
            dlg.DefaultExt = ".laa";
            dlg.Filter = "Launch All Around Playlist (.laa)|*.laa";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                // Load à implémenter
                string filename = dlg.FileName;
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                PlaylistModel pl = (PlaylistModel)formatter.Deserialize(stream);
                stream.Close();
                ((PlaylistModel)this.model).Reset(pl);
            }
        }
    }
}
